#!/bin/sh

. /usr/share/common

config="/etc/imp.conf"

check_imp() {
	local isp_path

	case "$SOC_FAMILY" in
		t10 | t20)
			isp_path="/proc/jz/isp/isp_info" ;;
		*)
			isp_path="/proc/jz/isp/isp-fs" ;;
	esac

	echo_info "Connecting to libimp-control"
	local i=10
	while [ "$i" -gt 0 ]; do
		if grep -Eq "running|Software" $isp_path; then
			imp-control getdeviceid > /dev/null
			echo -n " "
			return
		fi
		i=$((i-1))
		echo -n "."
		sleep 1
	done
	echo_error "Timeout"
	exit 1
}

start() {
	echo_title "IMP Configuration"

	if is_streamer_disabled; then
		echo_error "Streamer disabled"
		exit 1
	fi

	check_imp

	if [ -f "$config" ]; then
		echo_info "Restore IMP Configuration"
		while read -r line; do
			imp-control $line > /dev/null
		done < $config
	else
		echo_warning "Nothing to restore"
	fi
}

case "$1" in
	start)
		start
		;;
	stop)
		true
		;;
	restart)
		stop
		start
		;;
	*)
		echo "Usage: $0 {start|stop|restart}"
		exit 1
		;;
esac

exit 0
